package com.example.sxt_practise.java并发编程;

import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

/**
 * @author gaofeng
 * date 2022年02月17日 23:15
 */
public class MailBox {

    private static Map<Integer, GuardedObject> map = new Hashtable<>() ;

    /**
     * 产生唯一id
     * @author gaofeng
     * @date  2022/2/17 23:15
     * @return int
     */
    private static int id = 1 ;
    private static synchronized int generateId() {
        return id ++ ;
    }

    public static GuardedObject createGuardedObject() {
        GuardedObject guardedObject = new GuardedObject(generateId()) ;
        map.put(guardedObject.getId(), guardedObject) ;
        return guardedObject ;
    }

    public static GuardedObject getObjById(Integer id) {
        return map.remove(id) ;
    }

    public static Set<Integer> getIds() {
        return map.keySet() ;
    }
}
